package com.paic.custserv.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.paic.custserv.vo.SysMenuVO;

public class MenuConverUtil {
	
	private final static String ROOT_MENU_KEY = "ROOT";
	
	public static List<SysMenuVO> fillMenuInfo(List<SysMenuVO> list) {
		Map<String, List<SysMenuVO>> menuMap = new HashMap<String, List<SysMenuVO>>();
		if(list != null) {
			for(SysMenuVO menu : list) {
				String key = String.valueOf(menu.getParentResourceId());
				if(CommonUtil.isBlank(key) || "0".equals(key)) {
					key = ROOT_MENU_KEY;
				}
				List<SysMenuVO> childMenuList = menuMap.get(key);
				if(childMenuList == null) {
					childMenuList = new ArrayList<SysMenuVO>();
				}
				childMenuList.add(menu);
				menuMap.put(key, childMenuList);
			}
			
			for(SysMenuVO menu : list) {
				menu.setSubMenuList(menuMap.get(String.valueOf(menu.getIdResource())));
				if(!CommonUtil.isNullOrEmpty(menu.getSubMenuList())) {
					menu.setHasChild(true);
				}
			}
			list = menuMap.get(ROOT_MENU_KEY);
		}
		return list;
	}

}
